var _base64 = new Base64();
var vm = new Vue({
  el: '#app',
  data: function () {
    return {
      loading: false, // loading
      stepActive: 0, //步骤
      verifyCode: '', //生成的验证码
      pdForm: {
        loginName: '', // 用户登陆名
        imageCode: '', //图片验证码
        verifyCode: '', //邮箱验证码
        newPassword: '', //新密码
        newPasswordTmp: '', //新密码副本
      },
      pdRules: {},
    }
  },
  methods: {
    // 获取验证码
    getCode: function (code) {
      console.log(code)
      this.verifyCode = code;
    },

    // 校验某一项
    checkItem: function (rule, value, callback) {
      var _that = this;
      if (rule.field === 'loginName') {
        if (!value) {
          return callback(new Error('请输入'))
        };
        _that.loading = true;
        request('sso', {
          url: SSO_SERVER_URL + 'forgetpwd/checkLoginName',
          data: {
            loginName: value
          },
          type: 'get'
        }, function (data) {
          _that.loading = false;
          if (data.success && data.content) {
            callback();
          } else {
            callback(new Error('账号不存在'));
          }
        }, function (msg) {
          _that.loading = false;
          _that.$message.error(msg.message || '网络错误');
        })
      }
      if (rule.field === 'imageCode') {
        if (!value) {
          return callback(new Error('请输入'))
        } else {
          if (_that.pdForm.imageCode.toLocaleUpperCase() !== _that.verifyCode) {
            // _that.pdForm.imageCode = ''; //先清除输入的
            _that.$refs.vcRef.handleDraw();
            return callback(new Error('验证码不正确'))
          } else {
            callback();
          }
        }
      }
      if (['verifyCode', 'newPassword'].indexOf(rule.field) > -1) {
        if (!value) {
          return callback(new Error('请输入'))
        } else {
          callback();
        };
      }
      if (rule.field === 'newPasswordTmp') {
        if (!value) {
          return callback(new Error('请输入'))
        } else {
          if (_that.pdForm.newPassword !== _that.pdForm.newPasswordTmp) {
            return callback(new Error('两次密码输入不一致'))
          } else {
            callback();
          }
        }
      }
    },

    // 下一步操作
    toNextStepFn: function () {
      var _that = this,
        step = _that.stepActive;
      if (step === 0) {
        _that.$refs['lgForm'].validate(function (valid) {
          if (valid) {
            _that.stepActive++;
            _that.senCodeToEmailFn();
          } else {
            return false;
          }
        });
      }
      if (step === 1) {
        _that.$refs['lgForm1'].validate(function (valid) {
          if (valid) {
            _that.loading = true;
            request('sso', {
              url: SSO_SERVER_URL + 'forgetpwd/validateVerifyCode',
              data: {
                loginName: _that.pdForm.loginName,
                verifyCode: _that.pdForm.verifyCode
              },
              type: 'get'
            }, function (data) {
              _that.loading = false;
              if (data.success && data.content) {
                _that.stepActive++;
              } else {
                if (data.message && data.message === '验证码已过期。') {
                  confirmMsg('', '验证码已过期，是否重新发送验证码到您的邮箱？', function () {
                    _that.senCodeToEmailFn();
                  }, function () {})
                } else {
                  _that.$message.error(data.message || '验证码错误');
                }
              }
            }, function (msg) {
              _that.loading = false;
              _that.$message.error(msg.message || '网络错误');
            })
          } else {
            return false;
          }
        });
      }
      if (step === 2) {
        _that.$refs['lgForm2'].validate(function (valid) {
          if (valid) {
            _that.loading = true;
            request('sso', {
              url: SSO_SERVER_URL + 'forgetpwd/resetPassword',
              data: {
                loginName: _that.pdForm.loginName,
                newPassword: _base64.encode(_that.pdForm.newPassword),
                verifyCode: _that.pdForm.verifyCode
              },
              type: 'post'
            }, function (data) {
              _that.loading = false;
              if (data.success) {
                _that.$message.success('新密码设置成功');
                _that.stepActive++;
              } else {
                _that.$message.error(data.message || '新密码设置成功失败');
              }
            }, function (msg) {
              _that.loading = false;
              _that.$message.error(msg.responseJSON.message || '网络错误');
            })
          } else {
            return false;
          }
        });
      }
    },

    // 发送验证码逻辑api
    senCodeToEmailFn: function () {
      var _that = this;
      request('sso', {
        url: SSO_SERVER_URL + 'forgetpwd/sendVerificationCodeEmail',
        data: {
          loginName: _that.pdForm.loginName
        },
        type: 'get'
      }, function (data) {
        if (data.success) {
          _that.$notify({
            title: '提示',
            message: '已发送验证码到您的邮箱，请到邮箱中查看',
            duration: 5000
          });
        } else {
          _that.$message.error(data.message || '发送验证码到邮箱失败');
        }
      }, function (msg) {
        _that.$message.error(msg.responseJSON.message || '网络错误');
      })
    },

    // 跳转登录页
    toLoginFn: function () {
      window.history.go(-1);
    },

    /**********新密码等级校验-start***********/
    //测试某个字符是属于哪一类.
    CharMode: function (iN) {
      if (iN >= 48 && iN <= 57) //数字
        return 1;
      if (iN >= 65 && iN <= 90) //大写字母
        return 2;
      if (iN >= 97 && iN <= 122) //小写
        return 4;
      else
        return 8; //特殊字符
    },
    //计算出当前密码当中一共有多少种模式
    bitTotal: function (num) {
      var modes = 0;
      for (i = 0; i < 4; i++) {
        if (num & 1) modes++;
        num /= 2;
      }
      return modes;
    },
    //返回密码的强度级别
    checkStrong: function (sPW) {
      if (sPW.length <= 8)
        return 0; //密码太短
      var Modes = 0;
      for (i = 0; i < sPW.length; i++) {
        //测试每一个字符的类别并统计一共有多少种模式.
        Modes |= this.CharMode(sPW.charCodeAt(i));
      }
      return this.bitTotal(Modes);
    },
    //当用户放开键盘或密码输入框失去焦点时，根据不同的级别显示不同的颜色 
    pwStrength: function (pwd) {
      var O_color = "#eeeeee";
      var L_color = "#FF0000";
      var M_color = "#FF9900";
      var H_color = "#33CC00";
      var Lcolor = '',
        Mcolor = '',
        Hcolor = '';
      if (pwd == null || pwd == '') {
        Lcolor = Mcolor = Hcolor = O_color;
      } else {
        S_level = this.checkStrong(pwd);
        switch (S_level) {
          case 0:
            Lcolor = Mcolor = Hcolor = O_color;
          case 1:
            Lcolor = L_color;
            Mcolor = Hcolor = O_color;
            break;
          case 2:
            Lcolor = Mcolor = M_color;
            Hcolor = O_color;
            break;
          default:
            Lcolor = Mcolor = Hcolor = H_color;
        }
      }
      // console.log(Lcolor,Mcolor,Hcolor)
      // return
      var doc = document;
      doc.getElementById("strength_L").style.background = Lcolor;
      doc.getElementById("strength_M").style.background = Mcolor;
      doc.getElementById("strength_H").style.background = Hcolor;
    },
    /**********新密码等级校验-end***********/
  },
  mounted: function () {
    var _that = this;
    document.addEventListener('keydown', function (event) {
      if (event.keyCode == 13) {
        _that.toNextStepFn();
      }
    });
  },
})